const { useGlobalState } = require('jws-tools-wx');

const [getAuthToken, setAuthToken] = useGlobalState("AUTHTOKEN", {
  storage: "local"
});

/**
 * 验证token
 * @param {*} date 
 */
const tokenVerify = date => {
  const token = getAuthToken();
  if (!token) {
    wx.redirectTo({
      url: '/pages/auth/login/index'
    })
  }
}

/**
 * 获取用户信息
 */
const getAuthUser = () => {
  if (!getAuthToken()) {
    return null;
  }
  return {
    username: "zhoukaidong",
    age: "26"
  }
}

/**
 * 登录
 */
const login = (data = {}) => {
  //在这里调用接口获取token
  setAuthToken({
    token: 'wx3da7ad15540aa97d'
  });
  wx.redirectTo({
    url: '/pages/home/index?vcarname=p7'
  });
}

module.exports = {
  tokenVerify,
  setAuthToken,
  getAuthUser,
  login
}